Android 自定义验证码输入框(支持粘贴连续性)

您所在的位置:网站首页 miui12 验证码自动填充 Android 自定义验证码输入框(支持粘贴连续性)

Android 自定义验证码输入框(支持粘贴连续性)

2024-06-09 14:06| 来源: 网络整理| 查看: 265

最近做新项目的时候,遇到需要验证码输入框,为了防止重复造轮子,特地查阅github及网上资源,都发现没有一款合适当前需求。

需求

1、能自定义输入框的数量、输入类型、宽度、高度、文字颜色大小、间距、背景色、自定义焦点、下划线 2、支持长按粘贴弹窗或剪切板内容自动填充(粘贴连续性)

其中第2点是最为重要的,正是其他人没有这点,逼得自己弄一个

示例

别人的示例: 在这里插入图片描述 粘贴居然不支持连续性,只能粘贴第一个字符,所以用的有点难受

自己的示例: 在这里插入图片描述

原理

大致是Edittext + n* TextView,然后设置edittext字体跟背景颜色都为透明,隐藏光标

Edittext:监听edittext每次输入一个字符就赋值到对应的TextView上,然后在清空自己

下划线:在TextView下面添加View

光标:这里的每个TextView的焦点光标其实对View设置了ValueAnimator

粘贴:粘贴弹窗是自定义的PopupWindow

源码有详细注释,这里就不一一说明

Github

https://github.com/Wynsbin/VerificationCodeInputView

收藏与博客

https://mppmz2.coding-pages.com/2019/10/25/VerificationCodeInputView/

Gradle

Step 1. Add it in your root build.gradle at the end of repositories:

allprojects { repositories { ... maven { url 'https://jitpack.io' } } }

Step 2. Add the dependency:

dependencies { implementation 'com.github.Wynsbin:VerificationCodeInputView:1.0.2' } How to use In layout In Java Code VerificationCodeInputView view = findViewById(R.id.vciv_code); view.setOnInputListener(new VerificationCodeInputView.OnInputListener() { @Override public void onComplete(String code) { Toast.makeText(MainActivity.this, code, Toast.LENGTH_SHORT).show(); } @Override public void onInput() { } }); //清除验证码 view.clearCode(); Attributes name说明format默认值vciv_et_number输入框的数量integer4vciv_et_inputType输入框输入类型enum数字模式vciv_et_width输入框的宽度dimension40dpvciv_et_height输入框的高度dimension40dpvciv_et_text_color输入框文字颜色colorColor.BLACKvciv_et_text_size输入框文字大小dimension14spvciv_et_spacing输入框间距,不输入则代表平分dimensionvciv_et_background输入框背景色reference&colorColor.WHITEvciv_et_foucs_background输入框焦点背景色,不输入代表不设置reference&colorvciv_et_cursor_width输入框焦点宽度dimension2dpvciv_et_cursor_height输入框焦点高度dimension30dpvciv_et_cursor_color输入框焦点颜色color#C3C3C3vciv_et_underline_height输入框下划线高度dimension1dpvciv_et_underline_default_color输入框无焦点的下划线颜色color#F0F0F0vciv_et_underline_focus_color输入框有焦点的下划线颜色color#C3C3C3vciv_et_underline_show输入框下划线是否展示booleanfalse VCInputType name说明number数字模式numberPassword数字密码模式text字符模式textPassword字符密码模式 输入框背景色支持类型

1、@drawable/xxx

2、@color/xxx

3、#xxxxxx



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3